package lms.dwr.demo.web.action.user;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import lms.dwr.demo.common.Constants;
import lms.dwr.demo.common.ServiceException;
import lms.dwr.demo.common.WebException;
import lms.dwr.demo.model.ModelUser;
import lms.dwr.demo.service.ServiceUser;
import lms.dwr.demo.web.action.BaseAction;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionRedirect;

/**
 * The account action
 * 
 * @author Jeccy.Zhao
 *
 */
public class UserAction 
extends BaseAction 
{
	/**
	 * The LOGGER
	 */
	private static final Logger LOGGER = Logger.getLogger(UserAction.class);
	
	/**
	 * The user service
	 */
	private ServiceUser serviceUser;
	
	/**
	 * 
	 * @param userName
	 * @return
	 * @throws ServiceException 
	 */
	public Boolean accountNameExisted(String userName) throws ServiceException
	{
		return serviceUser.getAccountByUserName(userName) != null;
	}
	
	/**
	 * <b>[WebAction]</b> 
	 * <br/>
	 * The account logout operation.
	 * 
	 * @throws WebException 
	 */
	public ActionForward accountHomePageLogout (ActionMapping mapping, 
			ActionForm form, HttpServletRequest request, HttpServletResponse response) throws WebException
	{
		if(this.getCurrentUser(request) != null)
		{
			this.clearCurrentUser(request);
		}
		
		return new ActionForward("/user.do?action=accountHomePageLogin", true);
	}
	
	/**
	 * 用户登陆
	 */
	public ActionForward accountHomePageLogin (ActionMapping mapping, 
			ActionForm form, HttpServletRequest request, HttpServletResponse response) throws WebException
	{
		
		if(this.getCurrentUser(request) == null)
		{
			if(request.getParameter("do") != null)
			{
				try
				{
					ModelUser accountForm = (ModelUser) form;
					
					ModelUser account = this.serviceUser.getAccountByUserNameAndPwd(
							accountForm.getUserName(), accountForm.getPassword());
					
					String ucsynlogin = null;
					
					if (account != null && account.getUserStatus() == ModelUser.AccountStatus.ACTIVATION.getValue())
					{
						// set the account entity into session
						this.setCurrentUser(request, account);
						
						if (StringUtils.isNotEmpty(request.getParameter("redirect_to")))
						{
							return new ActionRedirect(new ActionForward(request.getParameter("redirect_to")));
						}
						else
						{
							if (isAjaxReq(request))
							{
								StringBuilder message = new StringBuilder();
								message.append(AjaxResponse.RESPONSE_SUCCESS);
								message.append(",");
								message.append(account.getUserType() == ModelUser.AccountType.ADMIN.getValue() ? "1" : "0");
								if (StringUtils.isNotEmpty(ucsynlogin))
								{
									message.append(",");
									message.append(ucsynlogin);
								}
								return ajaxPrint(response, message.toString());
							}
							else
							{
								return new ActionRedirect(new ActionForward("/admin/student.do?action=studentPageIndex"));
							}
						}
					}
					else
					{
						String errorMessage = "";
						if (account != null && account.getUserStatus() == ModelUser.AccountStatus.NOT_ACTIVATION.getValue())
						{
							errorMessage = this.getMessageFromResource(request, "user.notactivated");
						} 
						else 
						{
							errorMessage = this.getMessageFromResource(request, "login.failure");
						}
						
						if (isAjaxReq(request))
						{
							return ajaxPrint(response, errorMessage);
						}
						else
						{
							request.setAttribute(Constants.DefaultMessageParam, errorMessage);
							return mapping.findForward("user.login.page");
						}
					}
				}
				catch (ServiceException e)
				{
					LOGGER.error("Logon failed caused by raised exception!", e);
				}
				catch (Exception e)
				{
					throw new WebException("Logon failed caused by raised exception!", e);
				}
			}
			else
			{
				// DO LOGIN
				return isAjaxReq(request) ? ajaxPrint(response, AjaxResponse.RESPONSE_ERROR) : mapping.findForward("user.login.page");
			}
		}
		else
		{
			ModelUser account = this.getCurrentUser(request);
			if (account == null)
			{
				return isAjaxReq(request) ? ajaxPrint(response, AjaxResponse.RESPONSE_ERROR) : mapping.findForward("user.login.page");
			}
			
			return isAjaxReq(request) ? ajaxPrint(response, AjaxResponse.RESPONSE_SUCCESS) : 
				new ActionRedirect(new ActionForward("/admin/student.do?action=studentPageIndex"));
		}
		
		return isAjaxReq(request) ? ajaxPrint(response, AjaxResponse.RESPONSE_ERROR) : mapping.findForward("user.login.page");
	}
	
	public static Logger getLogger() 
	{
		return LOGGER;
	}
	
	public ServiceUser getServiceUser() 
	{
		return serviceUser;
	}

	public void setServiceUser(ServiceUser serviceUser) 
	{
		this.serviceUser = serviceUser;
	}

}
